home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_4 / lagran.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  735 b   |  23 lines  |  [MATF/MATL]

  1. function [C,L] = lagran(X,Y)
  2. % [C] = lagran(X,Y)
  3. % [C,L] = lagran(X,Y)
  4. % Construction of the collocation polynomial.
  5. % The method is Lagrange interpolation.
  6. % X is the list of abscissas, input.
  7. % Y is the list of ordinates, input.
  8. % W is the coefficient list for the polynomial, output.
  9. % L is the matrix for the Lagrange coefficient polynomials, output.
  10. n1 = length(X);
  11. n  = n1-1;
  12. L = zeros(n1,n1);
  13. for k=1:n+1,          % Form the Lagrange coefficient polynomials 
  14.   V = 1;              % by using poly(r1) to create a polynomial
  15.   for j=1:n+1,        % with a known root, and conv(P2,P1)
  16.     if k ~= j,        % which is polynomial multiplication.
  17.       V = conv(V,poly(X(j)))/(X(k)-X(j));
  18.     end
  19.   end
  20.   L(k,:) = V;
  21. end
  22. C = Y*L; 
  23.